package com.yikes.srb.controller.web;

import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.result.Result;
import com.yikes.srb.leadingModle.form.lend.LendAddForm;
import com.yikes.srb.leadingModle.form.lend.LendEditForm;
import com.yikes.srb.leadingModle.form.lend.LendQuery;
import com.yikes.srb.leadingModle.vo.LendVO;
import com.yikes.srb.service.LendService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的准备表 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@RequiredArgsConstructor
@Tag(name = "LendController", description = "标的准备表")
@RestController
@RequestMapping("/admin/lend")
public class LendController {

    private final LendService service;


    @Operation(description = "分页查询")
    @GetMapping("/list_page")
    public Result<PageResult<LendVO>> listPage(LendQuery query) {

        return Result.ok(service.listPage(query));
    }

    @Operation(description = "根据id查询单条信息")
    @GetMapping("/info/{id}")
    public Result<LendVO> infoId(@PathVariable("id") Long id) {

        return Result.ok(service.infoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/create")
    public Result create(@Valid @RequestBody LendAddForm form) {

        service.create(form);
        return Result.ok();
    }

    @Operation(description = "编辑")
    @PutMapping("/edit")
    public Result edit(@Valid @RequestBody LendEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "标的列表")
    @GetMapping("/list")
    public Result list() {

        List<LendVO> lendList = service.selectList();
        return Result.ok(lendList);
    }

    @Operation(description = "获取标的信息")
    @GetMapping("/show/{id}")
    public Result show(@PathVariable Long id) {

        Map<String, Object> result = service.getLendDetail(id);
        return Result.ok(result);
    }

    @Operation(description = "计算投资收益")
    @GetMapping("/getInterestCount/{invest}/{yearRate}/{totalmonth}/{returnMethod}")
    public Result getInterestCount(
            @Parameter(name = "投资金额", required = true)
            @PathVariable("invest") BigDecimal invest,

            @Parameter(name = "年化收益", required = true)
            @PathVariable("yearRate") BigDecimal yearRate,

            @Parameter(name = "期数", required = true)
            @PathVariable("totalmonth") Integer totalmonth,

            @Parameter(name = "还款方式", required = true)
            @PathVariable("returnMethod") Integer returnMethod) {

        BigDecimal interestCount = service.getInterestCount(invest, yearRate, totalmonth, returnMethod);
        return Result.ok(interestCount);
    }

    @Operation(description = "放款")
    @GetMapping("/makeLoan/{id}")
    public Result makeLoan(
            @Parameter(name = "标的id", required = true) @PathVariable("id") Long id) {

        service.makeLoan(id);
        return Result.ok("放款成功");
    }
}
